//
//  WUTaskViewController.m
//  Home
//
//  Created by 吴军虎吴军虎 on 16/6/2.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "WUTaskViewController.h"
#import "WUTaskViewVCell.h"
#import "YHYEditDetailViewController.h"
#import "YHYAddViewController.h"
#import "YHYMDetailTaskModel.h"

@interface WUTaskViewController ()<UITableViewDelegate,UITableViewDataSource,WUTaskViewVCellDelegate>
{
//    YHYMDetailTaskModel *model;
    NSString *url ;
}

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIButton  *button;
@property (nonatomic, retain) NSMutableArray *myArray;
@property (nonatomic, retain) YHYMDetailTaskModel *myModel;
@end

@implementation WUTaskViewController

-(void) viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=YES;
    if ([[LOCALSEVE objectForKey:@"SID"] isEqualToString:@"1"]) {
        
        url = [NSString stringWithFormat:@"%@%@",JIAFANURL,@"Gafaer/Task/myTaskList"];
         self.title=@"我发布的任务";
    }
    if ([[LOCALSEVE objectForKey:@"SID"] isEqualToString:@"1"]) {
        
        url = [NSString stringWithFormat:@"%@%@",JIAFANURL,@"Gafaer/Task/myTaskList"];
          self.title=@"我的案例";
        
    }
  
    self.navigationController.navigationBar.translucent = YES;
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _myArray = [NSMutableArray array];
    
    self.view.backgroundColor=LV;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.backgroundColor = GH;
    [self.view addSubview:_button];
    [self.button setTitle:@"添加新任务" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(goNext:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        make.centerXWithinMargins.mas_equalTo(self.view);
        make.height.mas_equalTo(40 * AUTO_HEIGHT);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    self.button.titleLabel.font = [UIFont fontWithName:@"Arial" size:LOGINFONT * TEXTWEIGHT];
    
    
    _myTableView = [UITableView new];
    _myTableView.backgroundColor = LV;
    [self.view addSubview:_myTableView];
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(63 * AUTO_HEIGHT);
        make.left.mas_equalTo(0 * AUTO_WIDTH);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-40 * AUTO_HEIGHT);
        
    }];
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    
    [_myTableView registerClass:[WUTaskViewVCell class] forCellReuseIdentifier:@"WUTaskViewVCell"];
    [_myTableView reloadData];
   

    [self getUrl];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_myArray.count != 0) {
        return _myArray.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
       static NSString *str =@"WUTaskViewVCell";
    WUTaskViewVCell *cell=[tableView dequeueReusableCellWithIdentifier:str forIndexPath:indexPath];
    cell.delegate=self;
    if (_myArray.count !=0) {
        cell.myModel = _myArray[indexPath.row];
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
     return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 760/3 * AUTO_HEIGHT;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YHYMDetailTaskModel *model = [YHYMDetailTaskModel new];
    model = [_myArray objectAtIndex:indexPath.row];
    
    
    NSString *Surl = [NSString stringWithFormat:@"%@%@",JIAFANURL,@"Gafaer/Task/viewTask"];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    NSString *code = [LOCALSEVE objectForKey:@"MCODE"];
    NSString *uid = [LOCALSEVE objectForKey:@"UID"];
    
    
    
    [manager GET:Surl parameters:@{@"mcode":code,@"uid":uid,@"tid":[NSString stringWithFormat:@"%ld",model.id]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"!!!!!!!%@",responseObject);
        NSMutableDictionary *dic = [responseObject objectForKey:@"data"];
        _myModel = [YHYMDetailTaskModel modelWithDictionary:dic];
        //       NSLog(@"%@",_myModel.title);
        //       NSLog(@"%@",_myModel.detail);
        YHYEditDetailViewController *edit=[[YHYEditDetailViewController alloc]init];
        [self.navigationController pushViewController:edit animated:YES];
        edit.myModel = _myModel;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
    }];
    

    
    
    
    
    

//    YHYMDetailTaskModel *dic = [self didCellgetUrl:model.id];
   

    

}

//- (YHYMDetailTaskModel *)didCellgetUrl:(NSInteger)myid{
//    NSString *url = [NSString stringWithFormat:@"%@%@",JIAFANURL,@"Gafaer/Task/viewTask"];
//    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
//    NSString *code = [LOCALSEVE objectForKey:@"MCODE"];
//    NSString *uid = [LOCALSEVE objectForKey:@"UID"];
//    
//    
//    
//    [manager GET:url parameters:@{@"mcode":code,@"uid":uid,@"tid":[NSString stringWithFormat:@"%ld",myid]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"!!!!!!!%@",responseObject);
//        NSMutableDictionary *dic = [responseObject objectForKey:@"data"];
//        _myModel = [YHYMDetailTaskModel modelWithDictionary:dic];
//        //       NSLog(@"%@",_myModel.title);
//        //       NSLog(@"%@",_myModel.detail);
//        
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//        NSLog(@"%@",error);
//    }];
//
//    return _myModel;
//}

- (void)getUrl{
    
   
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    NSString *code = [LOCALSEVE objectForKey:@"MCODE"];
    NSString *uid = [LOCALSEVE objectForKey:@"UID"];
    
    
    [manager GET:url parameters:@{@"mcode":code,@"uid":uid} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *array =[responseObject objectForKey:@"data"];
//        NSMutableDictionary *myDic = array[0];
        for (NSMutableDictionary *dic in array) {
            YHYMDetailTaskModel *model = [YHYMDetailTaskModel modelWithDictionary:dic];
            [_myArray addObject:model];
        }
        
        
        
        
        
        [_myTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error");
        
    }];
    
    
    

    
}

-(void) Back:(UIButton *) btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) taskView:(NSInteger)index
{
    if (index ==0) {
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除任务" preferredStyle:UIAlertControllerStyleAlert];

 
        [alertControl addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

            NSLog(@"确定按钮");
            
        }]];
        
        [alertControl addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        
        [self presentViewController:alertControl animated:YES completion:nil];
        
    }else{
    
        YHYAddViewController *add=[[YHYAddViewController alloc]init];
        [self.navigationController pushViewController:add animated:YES];

    }
}


-(void) goNext:(UIButton *) btn
{
    YHYAddViewController *add=[[YHYAddViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
}

@end
